package interfaces.hud.chat;

import main.InitGame;
import main.listeners.listenerClasses.LanguageListener;

import org.fenggui.ComboBox;
import org.fenggui.TextEditor;
import org.fenggui.event.ButtonPressedEvent;
import org.fenggui.event.IButtonPressedListener;
import org.fenggui.event.ISelectionChangedListener;
import org.fenggui.event.SelectionChangedEvent;
import org.fenggui.layout.StaticLayout;

import fileHandling.language.Lang;
import fileHandling.language.LanguageLoader;
import fileHandling.language.interfaces.OptionsWindowText;
import fileHandling.language.options.Controls;
import gameStates.absGamesStates.gui.AbsChatState;

import interfaces.GUISource;
import interfaces.language.LanguageButton;
import interfaces.superWidgets.InterfaceWindow;

public class ChatInputWindow extends InterfaceWindow {
		
	private TextEditor textEditor;
	private ComboBox comboBox;
	private LanguageButton button;
	private int widgetHeight, contentHeight;
	private ChatType currentType;
	private boolean setInternally;
	private AbsChatState chatState;
	
	public ChatInputWindow(AbsChatState chatState) {
		super(false, "");
		this.chatState = chatState;
		
		content.setLayoutManager(new StaticLayout());
		
		int width = chatState.getWidth() / 2;
		contentHeight = chatState.getHeight() / 10;
		int x = chatState.getWidth() / 4;
		int y = chatState.getHeight() / 3;
		
		setXY(x, y);
		setSize(width, contentHeight);
		
		layout();
		
		widgetHeight = content.getHeight() / 2;
		textEditor = new TextEditor();
		textEditor.setSize(content.getWidth(), widgetHeight);
		textEditor.setXY(0, widgetHeight);
		textEditor.setWordWarping(true);
		textEditor.setMultiline(false);
		GUISource.setTheme(textEditor);
		content.addWidget(textEditor);
		
		changeLanguage();
		
		button = new LanguageButton(OptionsWindowText.Ok, GUISource.smallFont);
		button.setSize(content.getWidth() / 2, widgetHeight);
		button.setX(content.getWidth() / 2);
		GUISource.setTheme(button);
		button.addButtonPressedListener(new IButtonPressedListener() {
			@Override
			public void buttonPressed(ButtonPressedEvent e) {
				submitText();
			}
		});
		content.addWidget(button);
		
		setChatType(ChatType.Chat);
		
		layout();
		
		InitGame.get().addLanguageListener(new LanguageListener() {
			@Override
			public void languageChanged() {
				changeLanguage();
			}
		});
	}
	
	protected void changeLanguage() {
		comboBox = GUISource.createComboBox(GUISource.middleFont);
		comboBox.setSize(content.getWidth(), widgetHeight);
		GUISource.setTheme(comboBox);
		content.addWidget(comboBox);
		
		comboBox.addItem(LanguageLoader.get(Controls.Chat));
		comboBox.addItem(LanguageLoader.get(Controls.Team_Chat));
		comboBox.addItem(LanguageLoader.get(Controls.Squad_Chat));
		comboBox.addSelectionChangedListener(new ISelectionChangedListener() {
			@Override
			public void selectionChanged(SelectionChangedEvent selectionChangedEvent) {
				if(setInternally) return;
				
				String selection = comboBox.getSelectedValue();
				ChatType type = null;
				if(selection.equals(LanguageLoader.get(Controls.Chat))) type = ChatType.Chat;
				else if(selection.equals(LanguageLoader.get(Controls.Team_Chat))) type = ChatType.TeamChat;
				else if(selection.equals(LanguageLoader.get(Controls.Squad_Chat))) type = ChatType.SquadChat;
				
				assert(type != null);
				setChatType(type);
			}
		});
	}
	
	public void setChatType(ChatType type) {
		chatState.getGUIDisplay().setFocusedWidget(textEditor);
		if(currentType == type) return;
		
		Lang lang = null;
		switch(type) {
			case Chat : lang = Controls.Chat; break;
			case TeamChat : lang = Controls.Team_Chat; break;
			case SquadChat : lang = Controls.Squad_Chat; break;
		}
		
		assert(lang != null);
		setInternally = true;
		comboBox.setSelected(LanguageLoader.get(lang));
		setInternally = false;
		currentType = type;
	}
	
 	public void submitText() {
 		String text = textEditor.getText();
 		if(!text.isEmpty() && !(text.length() == 1 && Character.isWhitespace(text.charAt(0)))) {
 			InitGame.get().getNetworkState().sendChatMessage(text, currentType);
 			clearInputField();
 		}

 		chatState.setActive(false);
 		InitGame.get().changeChatState(chatState);
	}
 	
 	public void clearInputField() {
 		textEditor.setText("");
 		textEditor.setHeight(widgetHeight);
		setHeight(contentHeight);
 		layout();
 	}
}